<?php
//**           MAIN API AUTOMATED UNIT TESTS                **//
//**                                                        **//
//**                                                        **//

require_once 'communalmeals_api.php';
require_once 'communalmeals_service.php';
require_once("DB.php");



//*******************************************************************//
//**                        SETUP DATABASE                         **//
//*******************************************************************//

class cmealsDBTEST implements iCmealsDB {
	
	private $db;
	
	function __construct() {
		$dsn = 'mysql://root:123@localhost/test';
		$options = array(
//			'debug' => 2,
		);
		$this->db =& DB::connect( $dsn, $options );
		if (PEAR::isError($this->db)) { die($this->db->getMessage()); }
		$this->db->setFetchMode(DB_FETCHMODE_OBJECT);
		$this->createTables();
	}
	
	public function addPrefix($table) {
		return "test_cmeals_".$table;
	}
	
	public function quote($value) {
		return $this->db->quoteSmart($value);
	}
	
	public function readQuery($query) {
		$res =& $this->db->getAll($query);
		$retObj = new cmealsDBResult();
		if (PEAR::isError($res)) {
			$retObj->isError = true;
			$retObj->errorMsg = $res->getMessage();
		}
		else {
			$retObj->result = $res;
		}
		return $retObj;
	}
	
	public function readQuerySingle($query) {
		$retObj = $this->readQuery($query);
		if (!$retObj->isError) {
			// check array has one entry and replace result with this
			if (is_array($retObj->result) && (count($retObj->result) > 0)) {
				$retObj->result = $retObj->result[0];
			}
			else {
				$retObj->result = null;
			}
		}
		return $retObj;
	}
	
	public function readQueryValue($query) {
		$res =& $this->db->getOne($query);
		$retObj = new cmealsDBResult();
		if (PEAR::isError($res)) {
			$retObj->isError = true;
			$retObj->errorMsg = $res->getMessage();
		}
		else {
			$retObj->result = $res;
		}
		return $retObj;
	}
	
	public function writeQuery($query) {
		$res =& $this->db->query($query);
		$retObj = new cmealsDBResult();
		if (PEAR::isError($res)) {
			$retObj->isError = true;
			$retObj->errorMsg = $res->getMessage();
		}
		else {
			$retObj->result = $this->db->affectedRows();
		}
		return $retObj;
	}
	
	private function createTables() {

		echo "Dropping tables...<br/>";

		$res =& $this->db->query(
			"DROP TABLE IF EXISTS test_cmeals_eventgroup;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"DROP TABLE IF EXISTS test_cmeals_event;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"DROP TABLE IF EXISTS test_cmeals_member;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"DROP TABLE IF EXISTS test_cmeals_crew;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"DROP TABLE IF EXISTS test_cmeals_team;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"DROP TABLE IF EXISTS test_cmeals_teammember;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"DROP TABLE IF EXISTS test_cmeals_signup;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"DROP TABLE IF EXISTS test_cmeals_autosignup;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		echo "Creating tables...<br/>";

		$res =& $this->db->query(
			"CREATE TABLE IF NOT EXISTS test_cmeals_eventgroup (
					id varchar(10) NOT NULL,
					description varchar(50),
					description_html text,
					organiser varchar(10),
					organiser_description varchar(50),
					accountant varchar(10),
					accountant_description varchar(50),
					deleted date,
					changed timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
					created timestamp,
					PRIMARY KEY (id)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"CREATE TABLE IF NOT EXISTS test_cmeals_event (
					id varchar(10) NOT NULL,
					group_id varchar(10) NOT NULL,
					type varchar(2) NOT NULL,
					subtype varchar(2) NOT NULL,
					status varchar(2) NOT NULL,
					date date NOT NULL,
					time time,
					description varchar(50),
					description_html text,
					closing_date date,
					closing_time time,
					organiser varchar(10),
					organiser_description varchar(50),
					accountant varchar(10),
					accountant_description varchar(50),
					changed timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
					created timestamp,
					PRIMARY KEY (id, group_id)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"CREATE TABLE IF NOT EXISTS test_cmeals_member (
					id varchar(10) NOT NULL,
					name varchar(50) NOT NULL,
					changed timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
					created timestamp,
					PRIMARY KEY (id)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"CREATE TABLE IF NOT EXISTS test_cmeals_crew (
					member_id varchar(10) NOT NULL,
					group_id varchar(10) NOT NULL,
					event_id varchar(10) NOT NULL,
					member_description varchar(50),
					changed timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
					created timestamp,
					PRIMARY KEY (member_id, group_id, event_id)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"CREATE TABLE IF NOT EXISTS test_cmeals_team (
					id varchar(10) NOT NULL,
					description varchar(50),
					changed timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
					created timestamp,
					PRIMARY KEY (id)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"CREATE TABLE IF NOT EXISTS test_cmeals_teammember (
					team_id varchar(10) NOT NULL,
					member_id varchar(10) NOT NULL,
					member_description varchar(50),
					changed timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
					created timestamp,
					PRIMARY KEY (team_id, member_id)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"CREATE TABLE IF NOT EXISTS test_cmeals_signup (
					member_id varchar(10) NOT NULL,
					group_id varchar(10) NOT NULL,
					event_id varchar(10) NOT NULL,
					category varchar(2) NOT NULL,
					participants smallint unsigned NOT NULL,
					changed timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
					created timestamp,
					PRIMARY KEY (member_id, group_id, event_id, category)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		$res =& $this->db->query(
			"CREATE TABLE IF NOT EXISTS test_cmeals_autosignup (
					member_id varchar(10) NOT NULL,
					event_type varchar(2) NOT NULL,
					event_subtype varchar(2) NOT NULL,
					category varchar(2) NOT NULL,
					participants smallint unsigned NOT NULL,
					changed timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
					created timestamp,
					PRIMARY KEY (member_id, event_type, event_subtype, category)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
		);
		if (PEAR::isError($res)) { die($res->getMessage()); }
		
		echo "Inserting test data into tables...<br/>";
		
		$member_id = 156;
		while ($member_id <= 208) {
			$res =& $this->db->query(
				"INSERT INTO test_cmeals_member (id, name, changed, created) VALUES ('".$member_id."', '".$member_id."Name', NULL, NULL);"
			);
			if (PEAR::isError($res)) { die($res->getMessage()); }
			$member_id = $member_id + 2;
		}
		
	}
	
}

CMeals::setDB(new cmealsDBTEST());


//*******************************************************************//
//**                     SETUP CONFIGURATION                       **//
//*******************************************************************//

class cmealsConfigTEST implements iCmealsConfig {
	
	public function getProperty($name) {
		return NULL;
	}
}

CMeals::setConfig(new cmealsConfigTEST());


//*******************************************************************//
//**                     SETUP ERROR HANDLER                       **//
//*******************************************************************//

class cmealsErrorTEST implements iCmealsError {
	
	public function handleInfo($code, $details = "") {
		echo "INFO: ".$code." ".$details."<br/>";
	}
	
	public function handleWarning($code, $details = "") {
		echo "WARNING: ".$code." ".$details."<br/>";
	}
	
	public function handleError($code, $details = "") {
		echo "ERROR: ".$code." ".$details."<br/>";
	}
}

CMeals::setErrorHandler(new cmealsErrorTEST());


//*******************************************************************//
//**                  SETUP MOCK JOOMLA CLASSES                    **//
//*******************************************************************//

class JFactory {
	
	public static function getDBO() {
		return null;
	}
	
}


//*******************************************************************//
//**                     ASSERTION FRAMEWORK                       **//
//*******************************************************************//

/**
 * array([0] => assertions tested, [1] => passed assertions)
 */ 
$assertionCount = array(0, 0); 

function assertTrue($bool, $msg="No details.") {
	global $assertionCount;
	$assertionCount[0]++;
	if (!$bool) {
		echo "Assertion failed. ". $msg."<br/>";
	}
	else {
		$assertionCount[1]++;
	}
}

function assertEquals($expected, $actual, $msg="No details.") {
	global $assertionCount;
	$assertionCount[0]++;
	if ($expected != $actual) {
		echo "Assertion failed. ".$msg." (expected != actual) ". $expected ." != ".$actual."<br/>";
	}
	else {
		$assertionCount[1]++;
	}
}

function assertionCount() {
	global $assertionCount;
	echo "Assertions tested: ".$assertionCount[0]."<br/>";
	echo "Assertions passed: ".$assertionCount[1]."<br/>";
}

//*******************************************************************//
//**                        PERFORM TESTS                          **//
//*******************************************************************//

// Test basic event group functionality

echo "Performing tests...<br/>";

echo " - event groups<br/>";

$eventGroup = CMeals::readEventGroup("XYZ");
assertEquals(null, $eventGroup);

$eventGroup = new cmeals_std_class();
$eventGroup->id = "XYZ";
CMeals::createEventGroup($eventGroup);

$eventGroup->description = "descr";
$eventGroup->description_html = "<html>descr</html>";
$eventGroup->organiser = "172";
$eventGroup->organiser_description = "Jorgen";
$eventGroup->accountant = "170";
$eventGroup->accountant_description = "Berit";
CMeals::updateEventGroup($eventGroup);

$eventGroup->id = "ZYX";
$eventGroup->organiser = "170";
CMeals::createEventGroup($eventGroup);

$eventGroup = CMeals::readEventGroup("XYZ");
assertEquals("172", $eventGroup->organiser);
$eventGroup = CMeals::readEventGroup("ZYX");
assertEquals("170", $eventGroup->organiser);

$eventGroupList = CMeals::readEventGroupList("description", 0, 10);
assertEquals(2, count($eventGroupList));

$eventGroupCount = CMeals::countEventGroups();
assertEquals(2, $eventGroupCount);

// test delta update
$oldEventGroup = CMeals::readEventGroup("XYZ");
$newEventGroup = new cmeals_std_class();
$newEventGroup->id = $oldEventGroup->id;
CMeals::updateEventGroupDelta($newEventGroup);
$newEventGroup = CMeals::readEventGroup("XYZ");
$oldEventGroupVars = get_object_vars($oldEventGroup);
$newEventGroupVars = get_object_vars($newEventGroup);
foreach (array_keys($oldEventGroupVars) as $varName) {
	assertEquals($oldEventGroupVars[$varName], $newEventGroupVars[$varName]);
}
$newEventGroup->description = "new descr";
CMeals::updateEventGroupDelta($newEventGroup);
$newEventGroup = CMeals::readEventGroup("XYZ");
assertEquals("new descr", $newEventGroup->description);

// Test basic event functionality
echo " - events<br/>";

$eventList = CMeals::readEventList("XYZ");
assertEquals(0, count($eventList));

$event = new cmeals_std_class();
$event->id = "ABC";
$event->group_id = "XYZ";
$event->type = CMeals::TYPE_COMMUNAL_MEAL;
$event->subtype = "MO";
$event->status = CMeals::STATUS_PLANNING;
$event->date = "2009-10-27";
CMeals::createEvent($event);

$event->time = "12:27";
$event->description = "descr";
$event->description_html = "<html>descr</html>";
$event->closing_date = "2009-10-28";
$event->closing_time = "20:30";
$event->organiser = "172";
$event->organiser_description = "Jorgen";
$event->accountant = "170";
$event->accountant_description = "Berit";
CMeals::updateEvent($event);

$event->status = CMeals::STATUS_PLANNING;
$event->group_id = "ZYX";
$event->id = "BAC";
$event->date = "2009-10-28";
CMeals::createEvent($event);
$event->date = "2009-11-01";
$event->id = "CBA";
$event->type = "XX";
CMeals::createEvent($event);

$eventList = CMeals::readEventList("XYZ");
assertEquals(1, count($eventList));
assertEquals(CMeals::TYPE_COMMUNAL_MEAL, $eventList[0]->type);
assertEquals("172", $eventList[0]->organiser);

$eventList = CMeals::readEventListByDate("2009-01-01", "2009-12-31");
assertEquals(3, count($eventList));
$eventList = CMeals::readEventListByDate("2009-10-26", "2009-10-26");
assertEquals(0, count($eventList));
$eventList = CMeals::readEventListByDate("2009-10-26", "2009-10-27");
assertEquals(1, count($eventList));
assertEquals("ABC", $eventList[0]->id);
$eventList = CMeals::readEventListByDate("2009-10-28", "2009-11-01");
assertEquals(2, count($eventList));
assertEquals("BAC", $eventList[0]->id);
assertEquals("CBA", $eventList[1]->id);
$eventList = CMeals::readEventListByDate("2009-10-28", "2009-11-01", CMeals::TYPE_COMMUNAL_MEAL);
assertEquals(1, count($eventList));
assertEquals("BAC", $eventList[0]->id);

// test delta update
$oldEvent = CMeals::readEvent("ABC", "XYZ");
$newEvent = new cmeals_std_class();
$newEvent->id = $oldEvent->id;
$newEvent->group_id = $oldEvent->group_id;
CMeals::updateEventDelta($newEvent);
$newEvent = CMeals::readEvent("ABC", "XYZ");
$oldEventVars = get_object_vars($oldEvent);
$newEventVars = get_object_vars($newEvent);
foreach (array_keys($oldEventVars) as $varName) {
	assertEquals($oldEventVars[$varName], $newEventVars[$varName]);
}
$newEvent->description = "new descr";
CMeals::updateEventDelta($newEvent);
$newEvent = CMeals::readEvent("ABC", "XYZ");
assertEquals("new descr", $newEvent->description);

// Test basic crew functionality

echo " - crew<br/>";

$crewList = CMeals::readCrewList("XYZ");
assertEquals(0, count($crewList));

$crew = new cmeals_std_class();
$crew->member_id = "172";
$crew->group_id = "XYZ";
CMeals::createCrew($crew);

$crewList = CMeals::readCrewList("XYZ");
assertEquals(1, count($crewList));
assertEquals("172", $crewList[0]->member_id);
assertEquals("172Name", $crewList[0]->name);

$crew->member_id = "170";
$crew->event_id = "ABC";
$crew->member_description = "Kristoffer";
CMeals::createCrew($crew);
$crewList = CMeals::readCrewList("XYZ", "ABC");
assertEquals(1, count($crewList));
assertEquals("170", $crewList[0]->member_id);
assertEquals("Kristoffer", $crewList[0]->member_description);

assertTrue(CMeals::isEventCrew("170", "XYZ", "ABC"), "isEventCrew 1");
assertTrue(CMeals::isEventCrew("172", "XYZ", "ABC"), "isEventCrew 2");
assertTrue(!CMeals::isEventCrew("999", "XYZ", "ABC"), "isEventCrew 3");
assertTrue(CMeals::isEventCrew("172", "XYZ", "BAC"), "isEventCrew 4");
assertTrue(!CMeals::isEventCrew("170", "XYZ", "BAC"), "isEventCrew 5");
assertTrue(!CMeals::isEventCrew("170", "ZYX", "ABC"), "isEventCrew 6");
assertTrue(CMeals::isEventGroupCrew("170", "XYZ"), "isEventGroupCrew 1");
assertTrue(CMeals::isEventGroupCrew("172", "XYZ"), "isEventGroupCrew 2");
assertTrue(!CMeals::isEventGroupCrew("999", "XYZ"), "isEventGroupCrew 3");
assertTrue(!CMeals::isEventGroupCrew("172", "ZYX"), "isEventGroupCrew 4");

// Test basic delete of event group/event/crew

echo " - delete<br/>";

$eventGroup->id = "XYZ";
CMeals::deleteEventGroup($eventGroup);
$eventGroup = CMeals::readEventGroup("XYZ");
assertTrue(is_null($eventGroup), "XYZ should be gone");
$eventGroup = CMeals::readEventGroup("ZYX");
assertEquals("ZYX", $eventGroup->id, "ZYX should not be gone");
$eventList = CMeals::readEventList("XYZ");
assertEquals(0, count($eventList));
$eventList = CMeals::readEventList("ZYX");
assertEquals(2, count($eventList));
$crewList = CMeals::readCrewList("XYZ");
assertEquals(0, count($crewList));

// Test with CMWK method

echo " - CMWK create method<br/>";

CMealsEvent::createEventGroupCMWK("2010", "14", array("MO", "TU"), array("172Name", "174Name"));
$eventGroup = CMeals::readEventGroup("CMWK201014");
assertEquals("2010/14", $eventGroup->description);
$eventList = CMeals::readEventList("CMWK201014");
assertEquals(2, count($eventList));
$event = CMeals::readEvent("MO", "CMWK201014");
assertEquals(CMeals::TYPE_COMMUNAL_MEAL, $event->type);
assertEquals("MO", $event->subtype);
$event = CMeals::readEvent("TU", "CMWK201014");
assertEquals(CMeals::TYPE_COMMUNAL_MEAL, $event->type);
assertEquals("TU", $event->subtype);
$crewList = CMeals::readCrewList("CMWK201014");
assertEquals(2, count($crewList));
assertEquals("172", $crewList[0]->member_id);
assertEquals("174", $crewList[1]->member_id);

$event = CMeals::readEvent("MO", "CMWK201014");
$event->status = CMeals::STATUS_OPEN;
CMeals::updateEvent($event);
$event = CMeals::readEvent("TU", "CMWK201014");
$event->status = CMeals::STATUS_OPEN;
CMeals::updateEvent($event);
$event = CMeals::readEvent("BAC", "ZYX");
$event->status = CMeals::STATUS_OPEN;
CMeals::updateEvent($event);

// Test signup

echo " - signup<br/>";

$signUp = new cmeals_std_class();
$signUp->group_id = "CMWK201014";
$signUp->event_id = "MO";
$signUp->member_id = "172";
$signUp->category = "A1";
$signUp->participants = 3;
CMeals::createSignUp($signUp);
$signUp->category = "A2";
$signUp->participants = 2;
CMeals::createSignUp($signUp);
$signUp->category = "A3";
$signUp->participants = 1;
CMeals::createSignUp($signUp);
$signUp->member_id = "170";
$signUp->category = "A1";
$signUp->participants = 4;
CMeals::createSignUp($signUp);
$signUp->category = "A2";
$signUp->participants = 0;
CMeals::createSignUp($signUp);
$signUp->category = "A3";
$signUp->participants = 5;
CMeals::createSignUp($signUp);
$signUp->participants = 6;
CMeals::updateSignup($signUp);
$signUp->event_id = "TU";
CMeals::createSignUp($signUp);
$signUp->group_id = "ZYX";
$signUp->event_id = "BAC";
CMeals::createSignUp($signUp);

$signUpList = CMeals::readSignUpList("CMWK201014");
assertEquals(7, count($signUpList));
$signUpList = CMeals::readSignUpList("CMWK201014", "MO");
assertEquals(6, count($signUpList));
assertEquals("170", $signUpList[0]->member_id);
assertEquals("A2", $signUpList[1]->category);
assertEquals(0, $signUpList[1]->participants);
assertEquals(6, $signUpList[2]->participants);
$signUpList = CMeals::readSignUpList("CMWK201014", "MO", "172");
assertEquals(3, count($signUpList));
assertEquals("172", $signUpList[0]->member_id);
assertEquals("A2", $signUpList[1]->category);
assertEquals(1, $signUpList[2]->participants);

CMeals::deleteSignUpByFields("CMWK201014", "MO", "172");
$signUpList = CMeals::readSignUpList("CMWK201014");
assertEquals(4, count($signUpList));
CMeals::deleteSignUpByFields("CMWK201014", "MO");
$signUpList = CMeals::readSignUpList("CMWK201014");
assertEquals(1, count($signUpList));
CMeals::deleteSignUpByFields("CMWK201014");
$signUpList = CMeals::readSignUpList("CMWK201014");
assertEquals(0, count($signUpList));
$signUpList = CMeals::readSignUpList("ZYX");
assertEquals(1, count($signUpList));

// Test auto-signup

echo " - auto signup<br/>";

$autoSignUp = new cmeals_std_class();
$autoSignUp->member_id = "172";
$autoSignUp->event_type = CMeals::TYPE_COMMUNAL_MEAL;
$autoSignUp->event_subtype = "MO";
$autoSignUp->category = "A1";
$autoSignUp->participants = 3;
CMeals::createAutoSignUp($autoSignUp);
$autoSignUp->category = "A2";
$autoSignUp->participants = 0;
CMeals::createAutoSignUp($autoSignUp);
$autoSignUp->category = "A3";
$autoSignUp->participants = 1;
CMeals::createAutoSignUp($autoSignUp);
$autoSignUp->event_subtype = "TU";
$autoSignUp->category = "A1";
$autoSignUp->participants = 5;
CMeals::createAutoSignUp($autoSignUp);
$autoSignUp->category = "A2";
$autoSignUp->participants = 4;
CMeals::createAutoSignUp($autoSignUp);
$autoSignUp->category = "A3";
$autoSignUp->participants = 2;
CMeals::createAutoSignUp($autoSignUp);
$autoSignUp->member_id = "170";
$autoSignUp->event_subtype = "MO";
$autoSignUp->category = "A1";
$autoSignUp->participants = 7;
CMeals::createAutoSignUp($autoSignUp);
$autoSignUp->category = "A2";
$autoSignUp->participants = 8;
CMeals::createAutoSignUp($autoSignUp);
$autoSignUp->category = "A3";
$autoSignUp->participants = 10;
CMeals::createAutoSignUp($autoSignUp);
$autoSignUp->participants = 9;
CMeals::updateAutoSignup($autoSignUp);

$autoSignUpList = CMeals::readAutoSignUpList("170", CMeals::TYPE_COMMUNAL_MEAL);
assertEquals(3, count($autoSignUpList));
assertEquals(9, $autoSignUpList[2]->participants);
$autoSignUpList = CMeals::readAutoSignUpList("172");
assertEquals(6, count($autoSignUpList));
$autoSignUpList = CMeals::readAutoSignUpList("172", CMeals::TYPE_COMMUNAL_MEAL, "MO");
assertEquals(3, count($autoSignUpList));
$autoSignUpList = CMeals::readAutoSignUpList("172", CMeals::TYPE_COMMUNAL_MEAL, "MO", "A2");
assertEquals(1, count($autoSignUpList));
assertEquals(0, $autoSignUpList[0]->participants);

$event = CMeals::readEvent("MO", "CMWK201014");
CMeals::createSignUpsFromAutoSignUps($event);

$signUpList = CMeals::readSignUpList("CMWK201014");
assertEquals(6, count($signUpList));
$signUpList = CMeals::readSignUpList("CMWK201014", "MO", "172");
assertEquals(3, count($signUpList));
assertEquals("172", $signUpList[0]->member_id);
assertEquals("A1", $signUpList[0]->category);
assertEquals(3, $signUpList[0]->participants);
assertEquals("A2", $signUpList[1]->category);
assertEquals(0, $signUpList[1]->participants);
assertEquals("A3", $signUpList[2]->category);
assertEquals(1, $signUpList[2]->participants);
$signUpList = CMeals::readSignUpList("CMWK201014", "MO", "170");
assertEquals(3, count($signUpList));
assertEquals("170", $signUpList[0]->member_id);
assertEquals("A1", $signUpList[0]->category);
assertEquals(7, $signUpList[0]->participants);
assertEquals("A2", $signUpList[1]->category);
assertEquals(8, $signUpList[1]->participants);
assertEquals("A3", $signUpList[2]->category);
assertEquals(9, $signUpList[2]->participants);

// Test cleanup

echo " - cleanup<br/>";

$eventGroup = CMeals::readEventGroup("CMWK201014");
CMeals::deleteEventGroup($eventGroup);
$deletedEventGroupList = CMeals::readDeletedEventGroupList();
assertEquals(1, count($deletedEventGroupList));
$tomorrow = date("Y-m-d", time() + 86400);
$deletedEventGroupList = CMeals::readDeletedEventGroupList($tomorrow);
assertEquals(1, count($deletedEventGroupList));
$yesterday = date("Y-m-d", time() - 86400);
$deletedEventGroupList = CMeals::readDeletedEventGroupList($yesterday);
assertEquals(0, count($deletedEventGroupList));

CMeals::updateEventStatus(CMeals::STATUS_CLOSED, "CMWK201014");
CMeals::updateEventStatus(CMeals::STATUS_BILLING, "CMWK201014");
assertEquals(6, count(CMeals::readSignUpList("CMWK201014")));
assertEquals(1, count(CMeals::readSignUpList("ZYX")));

CMeals::archiveEvents();
$allArchiveEventGroupList = CMeals::readEventGroupListByMinimumStatus(CMeals::STATUS_ARCHIVE);
assertEquals(1, count($allArchiveEventGroupList));
assertEquals("CMWK201014", $allArchiveEventGroupList[0]->id);
assertEquals(0, count(CMeals::readSignUpList("CMWK201014")));
assertEquals(1, count(CMeals::readSignUpList("ZYX")));



// Test member

echo " - members<br/>";

$memberList = CMeals::readMemberList();
assertEquals(27, count($memberList));
assertEquals("156", $memberList[0]->id);
assertEquals("156Name", $memberList[0]->name);

$member = CMeals::readMember("156");
assertEquals("156", $member->id);
assertEquals("156Name", $member->name);

// Test event type

echo " - event types<br/>";

$eventTypeList = CMeals::readEventTypeList();
assertEquals(1, count($eventTypeList));
assertEquals(CMeals::TYPE_COMMUNAL_MEAL, $eventTypeList[0]->id);
assertEquals("COMMUNAL_MEAL", $eventTypeList[0]->display_name);

// Test event sub-type

echo " - event sub-types<br/>";

$subtypeList = CMeals::readEventSubTypeList(CMeals::TYPE_COMMUNAL_MEAL);
assertEquals(7, count($subtypeList));
assertEquals("MO", $subtypeList[0]->id);
assertEquals("MONDAY", $subtypeList[0]->display_name);
assertEquals("SU", $subtypeList[6]->id);
assertEquals("SUNDAY", $subtypeList[6]->display_name);

// Test category

echo " - event categories<br/>";

$categoryList = CMeals::readEventCategoryList(CMeals::TYPE_COMMUNAL_MEAL);
assertEquals(3, count($categoryList));
assertEquals("AD", $categoryList[0]->id);
assertEquals("ADULT", $categoryList[0]->display_name);
assertEquals("CH", $categoryList[1]->id);
assertEquals("IN", $categoryList[2]->id);

// Test teams

echo " - teams<br/>";

$team = new cmeals_std_class();
$team->id = "TEAM1";
$team->description = "BTEAM";
CMeals::createTeam($team);
$team->id = "TEAM2";
$team->description = "ATEAM";
CMeals::createTeam($team);

$teamList = CMeals::readTeamList();
assertEquals(2, count($teamList));
assertEquals("TEAM2", $teamList[0]->id);
assertEquals("TEAM1", $teamList[1]->id);
assertEquals("ATEAM", $teamList[0]->description);

//*******************************************************************//
//**                          FINALIZE                             **//
//*******************************************************************//

assertionCount();

?>